package cn.b.sky.mes.qm.controller;

import java.util.List;

import cn.b.sky.mes.qm.model.Defect;
import cn.b.sky.mes.qm.model.DefectInspectRecord;
import cn.b.sky.mes.qm.model.DefectProcessOperation;
import cn.b.sky.mes.qm.model.InspectRecordAttachment;
import cn.b.sky.mes.qm.service.DefectAttributeService;
import cn.b.sky.mes.qm.service.DefectGraphService;
import cn.b.sky.mes.qm.service.DefectService;
import cn.b.sky.mes.qm.service.InspectRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.service.ProcessService;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.pp.enums.OrderStatusCode;
import cn.b.sky.mes.pp.model.ProductionControl;
import cn.b.sky.mes.pp.model.condition.ProductionControlCondition;
import cn.b.sky.mes.pp.service.ProductionControlService;

/**
 * 
 * <strong>Title : DefectGraphController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.qm.controller<br></strong>
 * <strong>Description : </strong>检验缺陷图形化查询控制器<br> 
 * <strong>Create on : 2015-12-16 上午11:08:19<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/defectGraph/*")
public class DefectGraphController extends SecurityController {

  /**
   * 图形化检验查询主界面
   */
  private static final String DEFECT_GRAPH_PAGE = "qm/defectGraph/defectGraph_panel";

  /**
   * 缺陷信息界面
   */
  private static final String DEFECT_DETAIL_PAGE = "qm/defectGraph/defectDetail";

  /**
   * 缺陷附件界面
   */
  private static final String ATTACHMENT_INFO_PAGE = "qm/defectGraph/attachmentInfo";

  @Autowired
  DefectGraphService defectGraphService;

  @Autowired
  WorkCenterService workCenterService;

  @Autowired
  ProductionControlService productionControlService;

  @Autowired
  InspectRecordService inspectRecordService;

  @Autowired
  DefectService defectService;

  @Autowired
  DefectAttributeService defectAttributeService;

  @Autowired
  ProcessService processService;

  @RequestMapping("defectGraph_search")
  public String initializationPage(Model model) {
    model.addAttribute("workCenters", workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), null));
    return DEFECT_GRAPH_PAGE;
  }

  @RequestMapping("selectProductionDetails")
  @ResponseBody
  public DWZResult queryProductionOrderDetails(ProductionControlCondition productionControlCondition) {
    if (StringUtils.isEmpty(productionControlCondition.getSiteCode())) {
      productionControlCondition.setSiteCode(getSiteCode());
    }
    productionControlCondition.setStatuseCode(OrderStatusCode.CREATE.getValue());
    return ajaxDone(productionControlService.selectProductionControlListForDefectGraph(productionControlCondition));
  }

  @RequestMapping("selectProcessOperations")
  @ResponseBody
  public DWZResult queryProcessOperations(ProductionControl productionControl) {
    return ajaxDone(defectGraphService.selectDefectProcessOperationsForGraph(productionControl));
  }

  @RequestMapping("selectInspectRecords")
  @ResponseBody
  public DWZResult queryDefectInspectRecords(DefectProcessOperation processOperation) {
    return ajaxDone(inspectRecordService.selectDefectInspectRecords(processOperation));
  }

  @RequestMapping("defectDetail_search")
  public String queryDefectDetail(Model model, DefectInspectRecord inspectRecord) {
    ProductionControl productionControl = productionControlService.selectProductionControl(inspectRecord.getSiteCode(), inspectRecord.getProductionControlNum());
    List<InspectRecordAttachment> attachments = inspectRecordService.getInspectRecordAttachment(inspectRecord.getId());
    inspectRecord.setAttachments(attachments);
    Defect defect = defectService.selectDefectOnly(inspectRecord.getDefectCode(), inspectRecord.getSiteCode());
    defect.setDefectAttributeValue(defectAttributeService.getDefectAttributeValuesForGraph(
        inspectRecord.getDefectCode(), inspectRecord.getSiteCode()));
    model.addAttribute("orderDetail", productionControl);
    model.addAttribute("defect", defect);
    model.addAttribute("inspectRecord", inspectRecord);
    return DEFECT_DETAIL_PAGE;
  }

  @RequestMapping("attachmentInfo_search")
  public String queryAttachmentInfo(Model model, Long inspectRecordId) {
    List<InspectRecordAttachment> attachments = inspectRecordService.getInspectRecordAttachment(inspectRecordId);
    model.addAttribute("attachments", attachments);
    return ATTACHMENT_INFO_PAGE;
  }

}
